<?php
// +----------------------------------------------------------------------
// Csmmeet [ Csmonline ]
// Author: chensm <chenshiming0802@163.com>
// Create by chensm at 2020-03-31
// +----------------------------------------------------------------------
namespace app\admin\controller\csmonline;

use addons\csmonline\library\CsmonlineCron;
use app\common\controller\Backend;

/**
 * Tables
 * http://127.0.0.1/fastadmin_plugin_csmmeet/public/q3HJDu2RgE.php/csmonline/tableonline/index
 * EG:https://www.cnblogs.com/bymo/p/8612724.html
 */
class Tableonline extends Backend
{

    protected $model = null;

    protected $relationSearch = true;

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $config = get_addon_config("csmonline");
            $heartseconds = (int) $config["heartseconds"];

            CsmonlineCron::generatefauser();
            CsmonlineCron::calcuserlog();

            $today = date("Y-m-d");

            $firstid = CsmonlineCron::getUserLogFirstId($today);

            list ($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $dao = new \app\admin\model\csmonline\Userlog();
            $total = $dao->alias('t')
                ->where("t.id", ">=", $firstid)
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->join("user a", "t.user_id=a.id", "left")
                ->field("t.isloginuser,t.userid,t.user_id,a.username,a.email,a.mobile,t.calccountry,t.calcprovince,t.calccity,t.calismobile,max(durationlasttime) visittime,count(*) visitpages,sum(durationseconds) durationseconds")
                ->group("t.isloginuser,t.userid,t.user_id,a.username,a.email,a.mobile,t.calccountry,t.calcprovince,t.calccity,t.calismobile")
                ->count();

            $list = $dao->alias('t')
                ->where("t.id", ">=", $firstid)
                ->where("durationlasttime", ">", time() - 2 * $heartseconds - 10)
                ->join("user a", "t.user_id=a.id", "left")
                ->field("t.isloginuser,t.userid id,t.userid,t.user_id,a.username,a.email,a.mobile,t.calccountry,t.calcprovince,t.calccity,t.calismobile,max(t.createtime) maxcreatetime,max(durationlasttime) visittime,count(*) cnt,sum(durationseconds) durationseconds")
                ->group("t.isloginuser,t.userid,t.user_id,a.username,a.email,a.mobile,t.calccountry,t.calcprovince,t.calccity,t.calismobile")
                ->order("maxcreatetime", "desc")
                ->limit($offset, $limit)
                ->select();

            $result = array(
                "total" => $total,
                "rows" => $list
            );
            return json($result);
        }
        return $this->view->fetch();
    }
}
